/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xthread.h
 *
 * Purpose: thead wrapper API define and implmenetation 
 *
 * Developer:
 *   wen.gu , 2023-07-07
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_CORE_XTHREAD_H__
#define __XBUS_CORE_XTHREAD_H__

#include <string>
#include <memory>
#include <functional>
#include <string_view>

namespace xbus::core {

class XThread {
public:
    using Runnable = std::function<void()>;
    using XThreadPtr = std::shared_ptr<XThread>;
public:
    XThread(std::string_view name, Runnable&& runnable = nullptr);
    virtual ~XThread();

public:
    virtual bool start();
    virtual bool join();
    virtual bool is_started();
    virtual bool set_priority(int32_t level);
    virtual int32_t priority() const { return priority_;}
    virtual bool is_self() const;
    virtual bool set_name(const std::string& name);
    virtual const std::string& name() { return name_; }
    virtual void set_runnable(Runnable&& runnable) { runnable_ = std::move(runnable); }
protected:
    virtual void run() { 
        if (runnable_) {
            runnable_();
        }
    }
private:
    class Impl;
    int32_t priority_;
    std::unique_ptr<Impl> impl_;
    std::string name_;
    Runnable runnable_;
};



} //namespace xbus::core


#endif /** !__XBUS_CORE_XTHREAD_H__ */